IberiaUtil = {}

Fk:loadTranslationTable{
  ["@$currentDiscardArea"] = "弃区",
}

--- 获取弃区
---@param room Room
---@return integer[]
function IberiaUtil.getDisArea(room)
  return room:getBanner("@$currentDiscardArea") or {}
end

--- 设置弃区
---@param room Room
---@param cards integer[]
function IberiaUtil.setDisArea(room, cards)
  room:setBanner("@$currentDiscardArea", #cards > 0 and cards or 0)
end

--- 角色是否拥有折冲属性
---@param player Player
---@return boolean
function IberiaUtil.isRepeller(player)
  for _, g in ipairs({player.general, player.deputyGeneral}) do
    if Fk.generals[g] and Fk.generals[g].headnote and Fk.generals[g].headnote == "repeller" then
      return true
    end
  end
  return false
end

--- 角色是否拥有帷幄属性
---@param player Player
---@return boolean
function IberiaUtil.isCounsailor(player)
  for _, g in ipairs({player.general, player.deputyGeneral}) do
    if Fk.generals[g] and Fk.generals[g].headnote and Fk.generals[g].headnote == "counsailor" then
      return true
    end
  end
  return false
end

---@param playerA Player
---@param playerB Player
function IberiaUtil.isMate(playerA, playerB)
  if playerA == playerB then return true end
  if playerA.kingdom == "unknown" or playerB.kingdom == "unknown" then
    return false
  elseif playerA.kingdom == "wild" or playerB.kingdom == "wild" then
    return false
  else
    return playerA.kingdom == playerB.kingdom
  end
end

---@param player ServerPlayer
---@param skill_name string
function IberiaUtil.hideGeneralBySkill(player, skill_name)
  table.forEach({player.general, player.deputyGeneral}, function (g, i)
    if g ~= "" and table.contains(Fk.generals[g]:getSkillNameList(), skill_name) then
      player:hideGeneral( i > 1 )
    end
  end)
end

--- 轮椅

--- 拼点
---@param player ServerPlayer
---@param tos ServerPlayer[]
---@param skill_name string
---@param card Card?
IberiaUtil.Pindian = function (player, tos, skill_name, card)
  local pindian = player:pindian(tos, skill_name, card)
  return pindian
end

---@class DoubleComboBoxParams
---@field skill_name string @ 询问技能名（烧条时显示）
---@field choices? string[] @ 可选项
---@field all_choices? string[] @ 所有选项
---@field default_choice string @ 默认选项
---@field type? string @ "custom"
---@field qml_path? string @ "packages/iberia/qml/DoubleCombo"

--- 两个选项框的ComboBox
--- 得到的data是 "选项1,选项2"
---@param spec DoubleComboBoxParams
IberiaUtil.DoubleComboBox = function(spec)
  spec.skill_name = spec.skill_name or ""
  spec.choices = type(spec.choices) == "table" and spec.choices or Util.DummyTable
  spec.default_choice = spec.default_choice or spec.choices[1]
  spec.all_choices = type(spec.all_choices) == "table" and spec.all_choices or spec.choices
  spec.type = "custom"
  spec.qml_path = "packages/iberia/qml/DoubleCombo"
  return spec
end

--- 获取一名角色的议事结果
---@param player ServerPlayer @ 角色
---@param data DiscussionDataSpec @ 议事数据
---@return table<string, integer>? @ 意见、意见牌
function IberiaUtil.getDiscussionOpinion(player, data)
  local result = data.results[player]
  if result then
    return {result.opinion, result.toCards[1]}
  end
  return nil
end

--- 令一名角色调整手牌数
---@param player ServerPlayer @ 角色
---@param num number @ 数
---@param skill_name string @ 技能名
function IberiaUtil.adjustHand(player, num, skill_name)
  local h = player:getHandcardNum()
  if num > h then
    player:drawCards(num - h, skill_name)
  elseif num < h then
    player.room:askToDiscard(player, {
      skill_name = skill_name,
      min_num = h - num,
      max_num = h - num,
      cancelable = false,
      include_equip = false,
    })
  end
end

---@class AskToChoosePlayersAndChoiceParams: AskToUseActiveSkillParams
---@field targets ServerPlayer[] @ 可以选的目标范围
---@field min_num integer @ 最小值
---@field max_num integer @ 最大值
---@field choices string[] @ 选项
---@field skill_name string @ 技能名
---@field cancelable boolean? @ 是否可取消
---@field prompt string? @ 提示

--- 询问选择角色和选项
---@param player ServerPlayer
---@param spec AskToChoosePlayersAndChoiceParams
function IberiaUtil.askToChoosePlayersAndChoice(player, spec)
  local tos, op = {}, nil
  local targets = spec.targets
  if #targets == 0 then return {}, nil end
  local min_num = spec.min_num or 1
  local max_num = spec.max_num or #targets
  local choices = spec.choices or {}
  local skill_name = spec.skill_name
  local cancelable = spec.cancelable or true
  local prompt = spec.prompt or ("#AskToChoosePlayersAndChoice:::" .. skill_name)

  local _, ret = player.room:askToUseActiveSkill(player, {
    skill_name = "AskToChoosePlayersAndChoice",
    cancelable = cancelable,
    prompt = prompt,
    extra_data = {
      targets = table.map(targets, Util.IdMapper),
      min_num = min_num,
      max_num = max_num,
      choices = choices,
      skillName = skill_name,
    }
  })

  if ret then
    tos = ret.targets
    op = ret.interaction
  end

  return tos, op
end

Fk:loadTranslationTable{
  ["#AskToChoosePlayersAndChoice"] = "%arg：请询问选择角色和选项",
}

--- 超链接
local href_table = {
  ["调离"] = ":iberia__lure_tiger_description",
  ["合法目标数"] = ":iberia__able_targets_num",
  ["主要阶段"] = ":iberia__main_phase",
}

Fk:loadTranslationTable{
  [":iberia__lure_tiger_description"] = "调离：不计入座次、不能使用牌、防止受到伤害、防止失去体力、防止回复体力",
  [":iberia__able_targets_num"] = "合法目标数：你使用此牌时，可以被你指定为目标的角色数",
  [":iberia__main_phase"] = "主要阶段：判定阶段、摸牌阶段、出牌阶段、弃牌阶段的合集",
}

--- 创建超链接
---@param text string
---@return string
IberiaUtil.href = function(text)
  return string.format("<a href='%s' style='color:grey;'>%s</a>", href_table[text], text)
end

return IberiaUtil